using UnityEngine;
using QFramework;

namespace QFramework.FlyBird
{
    public partial class PipeGenerator : ViewController
    {
        public float DurationMin = 2;
        public float DurationMax = 3;
        public static SimpleObjectPool<Pipe> PipePool;
        void Start()
        {
            // Code Here
            PipeTemplate.Hide();
            mGeneraeTime = Time.time;
            mDuration = Random.Range(DurationMin, DurationMax);

            PipePool = new SimpleObjectPool<Pipe>(() =>
            {
                return PipeTemplate.Instantiate().Hide();
            }, (pipe) =>
            {
                pipe.ResetData();
            }, initCount: 5);
        }
        private float mGeneraeTime = 0;
        private float mDuration = 0;
        private void Update()
        {
            if (FlappyBird.GameState.Value == GameStates.NotStart)
            {
                return;
            }
            if (Time.time - mGeneraeTime > mDuration)
            {
                mGeneraeTime = Time.time;
                mDuration = Random.Range(DurationMin, DurationMax);
                var pipe = PipePool.Allocate();
                pipe.Position(PipeGeneratePos.position).LocalPositionY(Random.Range(3, -5)).Show();
            }
        }
        private void OnDestroy()
        {
            PipePool = null;
        }
    }
}
